package pt.iscte.pcd.project.userInterface;

import javax.swing.table.AbstractTableModel;

import pt.iscte.pcd.project.exceptions.FileSystemException;
import pt.iscte.pcd.project.remote.RemoteDirectory;
import pt.iscte.pcd.project.remote.RemoteEntry;
import pt.iscte.pcd.project.remote.RemoteFile;

public class TableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private RemoteDirectory selectedDirectory ;
	
	public TableModel(RemoteDirectory root) {
		setSelectedDirectory(root);
		notifyChanges();
		
	}

	// vai buscar a list a lista, e retira a root e chama o get List
	

	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		return getSelectedDirectory().getListFiles().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		RemoteEntry entrada = (RemoteEntry) getSelectedDirectory().getListFiles().get(rowIndex);
		switch(columnIndex){
		case 0:
			return entrada.getName();
		case 1:
			return entrada.getType();
		case 2:
			switch (entrada.getType()) {
			case FILE:
				
				try {
					return ((RemoteFile)entrada).length() ;
				} catch (FileSystemException e) {
					e.printStackTrace();
				}
			case DIRECTORY:
				return ((RemoteDirectory)entrada).lengthPaste() ;
			default:
				return 0;
			}
			
			default:
				return "";
			
		}
	}	
		
		
	
	@Override
	public String getColumnName(int column) {
		switch(column){
		case 0:
			return "Name";
		case 1:
			return "Type";
		case 2:
			return "Size";
		}
		return null;
	}

	public void notifyChanges(){
		fireTableDataChanged();
	}

	public RemoteDirectory getSelectedDirectory() {
		return selectedDirectory;
	}

	public void setSelectedDirectory(RemoteDirectory selectedDirectory) {
		this.selectedDirectory = selectedDirectory;
	}

	



}
